/*
 * Created on Feb 18, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.unash.z3950.query;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Ashwini Gokhale
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ExecuteQuery {

	
    public static void execute(String sql) {
			String driver = "com.mysql.jdbc.Driver";
			String url = "jdbc:mySubprotocol:myDataSource";
			Connection con;
			Statement stmt;
	
			try {
				Class.forName(driver);

			} catch(java.lang.ClassNotFoundException e) {
				System.err.print("ClassNotFoundException: ");
				System.err.println(e.getMessage());
			}
	
			try {
				con = DriverManager.getConnection("jdbc:mysql://localhost/unash");
	
				stmt = con.createStatement();					
	
				ResultSet rs = stmt.executeQuery(sql);
				ResultSetMetaData rsmd = rs.getMetaData();
				int numberOfColumns = rsmd.getColumnCount();
				int rowCount = 1;
				while (rs.next()) {
					System.out.println("Row " + rowCount + ":  ");
					for (int i = 1; i <= numberOfColumns; i++) {
						System.out.print("   Column " + i + ":  ");
						System.out.println(rs.getString(i));
					}
					System.out.println("");
					rowCount++;
				}
				stmt.close();
				con.close();

			} catch(SQLException ex) {
				System.err.print("SQLException: ");
				System.err.println(ex.getMessage());
			}	
		}
	}


//ExecuteQuery
